package org.xiaom.butler.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xiaom.butler.bean.User;
import org.xiaom.butler.service.BaseService;
import org.xiaom.butler.util.Util;

public class LoginServlet extends HttpServlet {
	BaseService service=new BaseService(User.class);
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name=Util.getQueryParam(req, "user_name", "");
		String pwd=Util.getQueryParam(req, "pass_word", "");
		String hql="select u from User u where u.userName='"+name+"' and u.passWord='"+pwd+"'";
		List<User> list=service.getList(hql);
		if(list.size()==1){
			req.getSession().setAttribute("user", list.get(0));
			resp.sendRedirect("index.jsp");
		}else{ 
			req.getSession().setAttribute("error_message", "用户名或密码错误");
			resp.sendRedirect("login.jsp");
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
